﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinStudent
{
    public partial class FrmEditSC : Form
    {
        public FrmEditSC()
        {
            InitializeComponent();
        }

        private string[] sc = null;
        private string sno = null;
        private string cno = null;

        private void FrmEditSC_Load(object sender, EventArgs e)
        {
            if (this.Tag!=null||(string)this.Tag!="")
            {
                sc = (this.Tag.ToString().Trim()).Split(',');
                sno = sc[0];
                cno = sc[1];

                string sqlsc = "select Sno,Cno,Grade from SC where Sno=@Sno and Cno=@Cno ";
                SqlParameter[] paras =
                 {
                    new SqlParameter("@Sno",sno),
                    new SqlParameter("@Cno",cno)
                };
                SqlDataReader dr = SqlHelper.ExecuteReader(sqlsc, paras);
                if (dr.Read())
                {
                    txtSno.Text = dr["Sno"].ToString().Trim();
                    txtCno.Text = dr["Cno"].ToString().Trim();
                    txtGrade.Text = dr["Grade"].ToString().Trim();
                }
                dr.Close();
            }
        }


        private void btnEdit_Click(object sender, EventArgs e)
        {
            string snonew = txtSno.Text.Trim();
            string cnonew = txtCno.Text.Trim();
            string grade = txtGrade.Text.Trim();
            if (string.IsNullOrEmpty(snonew))
            {
                MessageBox.Show("学号不能为空", "选课成绩信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(cnonew))
            {
                MessageBox.Show("课程号号不能为空", "选课成绩信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            {
                string sqlSC = "select count(*) from SC where Sno=@Sno and Cno=@Cno";
                SqlParameter[] paras =
                {
                    new SqlParameter("@Sno",sno),
                    new SqlParameter("@Cno",cno)
                };
                object o = SqlHelper.ExecuteScalar(sqlSC, paras);
                if (o!=null||o!=DBNull.Value||(int)o!=0)
                {
                    string sql = "update SC set Sno=@SnoNew,Cno=@CnoNew,Grade=@Grade where Sno=@Sno and Cno=@Cno ";
                    SqlParameter[] parasNew =
                    {
                        new SqlParameter("@SnoNew",snonew),
                        new SqlParameter("@CnoNew",cnonew),
                        new SqlParameter("@Grade",grade),
                        new SqlParameter("@Sno",sno),
                        new SqlParameter("@Cno",cno)
                    };
                    int count = SqlHelper.ExecutNonQuery(sql, parasNew);
                    if (count > 0)
                    {
                        MessageBox.Show("选课成绩信息修改成功", "选课成绩信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("选课成绩信息修改失败", "选课成绩信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("选课成绩信息无效", "选课成绩信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
